﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Eacademy.Library.Membership;
using Eacademy.Domain.Abstractions;
using Eacademy.Domain.Repository;
using Eacademy.Domain.Models;
using Eacademy.Library.Helpers;
using Eacademy.Domain.Services;
using System.Web.Security;
using System.Text;
using Ninject;
using Eacademy;
using System.Linq.Expressions;
using Microsoft.Web.Mvc;

namespace System.Web.Mvc
{


    public static class HtmlHelperExtensions
    {

        private static CustomMembershipProvider Provider
        {
            get
            {
                return (CustomMembershipProvider)Membership.Provider;
            }
        }


        public static string EditProfileLink(this HtmlHelper helper, string buttonText)
        {

            //string username = HttpContext.Current.User.Identity.Name;


            if (Provider.User != null)
            {
                return String.Format("<a href=\"/{0}/profile/edit\">{1}</a>", Provider.User.UserType, buttonText);
            }
            return String.Empty;

        }

        public static string UserEventsUrl(this HtmlHelper helper)
        {

            if (Provider.User != null)
            {
                //return String.Format("/user/{0}/events", Provider.User.UserId);
                return "/user/events";
            }
            return String.Empty;

        }

        public static string EventTitleLink(this HtmlHelper helper, long eventId, string title)
        {
            return String.Format("<a href=\"/events/details/{0}/{1}\">{2}</a>", eventId, title.GenerateSlug(), title);
        }

        public static string EventLink(this HtmlHelper helper, long eventId, string title)
        {
            return String.Format("/events/details/{0}/{1}", eventId, title.GenerateSlug());
        }

        public static string EventItemButton(this HtmlHelper htmlHelper, EventActionTypes eventActionType, bool isBookmarked, long eventId)
        {
            StringBuilder ul = new StringBuilder();

            switch (eventActionType)
            {
                case EventActionTypes.AdminEventList:
                    ul.AppendLine("<ul class=\"event-menu\"> ");
                    ul.AppendFormat("	<li><a href=\"/admin/events/edit/{0}\">Edit</a></li>", eventId);
                    ul.AppendFormat("	<li><a class=\"delete-event-link\" href=\"/admin/events/delete/{0}\">Delete</a></li> ", eventId);
                    ul.AppendLine("</ul> ");
                    break;
                case EventActionTypes.UserEventList:
                    ul.AppendLine("<ul class=\"event-menu\"> ");
                    ul.AppendFormat("	<li><a href=\"/user/events/edit/{0}\">Edit</a></li>", eventId);
                    ul.AppendFormat("	<li><a class=\"delete-event-link\" href=\"/user/events/delete/{0}\">Delete</a></li> ", eventId);
                    ul.AppendLine("</ul> ");
                    break;
                case EventActionTypes.EventBookmarks:
                case EventActionTypes.EventList:
                case EventActionTypes.EventSearch:
                    ul.AppendLine("<ul class=\"event-menu\"> ");
                    if (isBookmarked)
                    {
                        ul.AppendFormat("	<li><a class=\"bookmarked-event-link\" href=\"/user/event-bookmarks/remove/{0}\">Remove Bookmark</a></li>", eventId);
                    }
                    else
                    {
                        ul.AppendFormat("	<li><a class=\"bookmark-event-link\" href=\"/user/event-bookmarks/add/{0}\">Bookmark</a></li>", eventId);
                    }
                    ul.AppendFormat("	<li><a class=\"email-event-link\" href=\"/user/events/email/{0}\">Share</a></li> ", eventId);
                    ul.AppendLine("</ul> ");
                    break;
            }


            return ul.ToString();


        }

        public static string EventListEmptyMessage(this HtmlHelper helper, EventActionTypes eventActionType, string suggestion)
        {
            string message = String.Empty;
            int eventTypeId = QueryString.Current["et"].ToInteger();
            switch (eventActionType)
            {
                case EventActionTypes.EventList:
                    if (eventTypeId > 0)
                    {
                        message = "No events for this specified event type";
                    }
                    else
                    {
                        message = "No events for this month";
                    }
                    break;
                case EventActionTypes.EventSearch:
                    if (!String.IsNullOrEmpty(suggestion))
                    {
                        message = String.Format("Do you mean? <i><a href=\"/events/search?query={0}\">{1}</a></i>", suggestion, suggestion);
                    }
                    else
                    {
                        message = "No search results";
                    }
                    break;
                case EventActionTypes.AdminEventList:
                case EventActionTypes.UserEventList:
                    message = "No existing events";
                    break;
                case EventActionTypes.EventBookmarks:
                    message = "You don't have any bookmarked events";
                    break;
            }
            return message;
        }


        public static string ShareFacebookLink(this HtmlHelper helper)
        {
            return String.Format("http://www.facebook.com/sharer.php?u={0}", HttpUtility.UrlEncode(HttpUtility.UrlPathEncode("http://www.bilmoko.com" + helper.ViewContext.HttpContext.Request.Url.PathAndQuery)));
        }


        public static string EmailToFriendLink(this HtmlHelper helper, int eventId, string iconClass = "", string title = "Email to Friend")
        {
            return String.Format("	<a class=\"email-event-link\" href=\"/user/events/email/{0}\"><span class=\"{1}\">{2}</span></a> ", eventId, iconClass, title);
        }

        public static string ActionImage<T>(this HtmlHelper html, Expression<Action<T>> action, string imageRelativeUrl, string alt, object imageAttributes) where T : Controller
        {
            string image = html.Image(imageRelativeUrl, alt, imageAttributes).ToString();
            return string.Format("<a href=\"{0}\">{1}</a>", html.BuildUrlFromExpression<T>(action), image);
        }
    }
}